namespace PickGold.Charting
{
	using System;
	using System.ComponentModel;
	using System.Globalization;

	internal class ElementPositionConverter : ExpandableObjectConverter
	{
		public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
		{
			return ((sourceType == typeof(string)) || base.CanConvertFrom(context, sourceType));
		}

		public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
		{
			return ((destinationType == typeof(string)) || base.CanConvertTo(context, destinationType));
		}

		public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
		{
			string strA = value as string;
			if (strA == null)
			{
				return base.ConvertFrom(context, culture, value);
			}
			if (string.Compare(strA, "Auto", StringComparison.OrdinalIgnoreCase) == 0)
			{
				return new ElementPosition();
			}
			string[] strArray = strA.Split(new char[] { ',' });
			if (strArray.Length != 4)
			{
				throw new ArgumentException(SR.ExceptionElementPositionConverter);
			}
			return new ElementPosition(float.Parse(strArray[0], CultureInfo.CurrentCulture), float.Parse(strArray[1], CultureInfo.CurrentCulture), float.Parse(strArray[2], CultureInfo.CurrentCulture), float.Parse(strArray[3], CultureInfo.CurrentCulture));
		}

		public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
		{
			if (destinationType == typeof(string))
			{
				return ((ElementPosition)value).ToString();
			}
			return base.ConvertTo(context, culture, value, destinationType);
		}
	}
}

